// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// W [author] rhyme_lph
// A [email]  rhymelph@gmail.com
// R [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/login/change_password_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "changePasswordPage")
class ChangePasswordPage extends StatefulWidget {
  @override
  _ChangePasswordPageState createState() => _ChangePasswordPageState();
}

class _ChangePasswordPageState extends State<ChangePasswordPage>
    with PagePresenter<ChangePasswordPage, ChangePasswordProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.change_password,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h40,
            listBuilder<bool>(
              selector: (_, __) => __.isNext,
              builder: (BuildContext context, bool value, Widget child) =>
                  value == false
                      ? _buildTipText(
                          tip: S.current.enter_your_current_password)
                      : _buildTipText(tip: S.current.enter_your_new_password),
            ),
            AppSpaces.h20,
            listBuilder<Tuple2<bool, bool>>(
                builder: (BuildContext context, Tuple2<bool, bool> value,
                        Widget child) =>
                    InputField(
                      key: ValueKey('password'),
                      leading: SvgPicture.asset(
                        A.assetsImagesCreatePadlock2,
                        width: getDp(15),
                        height: getDp(15),
                      ),
                      tip: S.current.password,
                      isPassword: true,
                      hasError: value.item1 || value.item2,
                      helpText: value.item2
                          ? S.current.password_is_incorrect
                          : S.current.at_least_6_alphanumerical_characters,
                      controller: provider.passwordController,
                      focusNode: provider.passwordFocusNode,
                    ),
                selector: (_, __) =>
                    Tuple2(__.passwordError, __.passwordIncorrect)),
            AppSpaces.h40,
            listBuilder<Tuple2<bool, bool>>(
                builder: (BuildContext context, Tuple2<bool, bool> value,
                        Widget child) =>
                    NormalButton(
                      title: value.item2 ? S.current.update : S.current.verify,
                      onTap: value.item1 == true ? provider.onVerify : null,
                    ),
                selector: (_, __) => Tuple2(__.enable, __.isNext)),
          ],
        ),
      );

  Center _buildTipText({String tip}) {
    return Center(
      child: Text(
        tip,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}
